Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Like which(1) unix command. Find the first instance of an executable in the PATH.
The 'which' npm package is a simple utility to locate a command in the user's path, similar to the Unix 'which' command. It is used to find the first instance of a specified executable in the system path, which can be useful for ensuring that a required command-line tool is available and for invoking system commands from Node.js scripts.
Synchronous command search
This feature allows you to synchronously find the path to an executable in the system's PATH. The 'sync' method returns the path to the specified executable if it exists.
const which = require('which');
const cmdPath = which.sync('node');
console.log(`Node.js is located at: ${cmdPath}`);
Asynchronous command search
This feature allows you to asynchronously find the path to an executable in the system's PATH. The 'which' function takes a callback that receives the path to the specified executable if it exists.
const which = require('which');
which('node', function (err, cmdPath) {
if (err) throw err;
console.log(`Node.js is located at: ${cmdPath}`);
});
Throw on not found
This feature allows you to specify whether the 'which' method should throw an error if the command is not found. By default, or when 'nothrow' is set to false, it will throw an error.
const which = require('which');
try {
const cmdPath = which.sync('unknown-command', {nothrow: false});
} catch (e) {
console.error('Command not found');
}
The 'find-exec' package is similar to 'which' in that it helps you find the path to executables in your PATH. However, it has a different API and may offer additional options for searching.
The 'lookpath' package is another alternative to 'which' that provides a way to check for the existence of an executable in the system's PATH. It is designed to be a simple and lightweight solution with a promise-based API.
The 'command-exists' package is used to check if a given command-line tool is available on the system's PATH. Unlike 'which', it does not return the path to the executable but simply a boolean indicating whether the command exists.
Like the unix which
utility.
Finds the first instance of a specified executable in the PATH
environment variable. Does not cache the results, so hash -r
is not
needed when the PATH changes.
var which = require('which')
// async usage
which('node', function (er, resolvedPath) {
// er is returned if no "node" is found on the PATH
// if it is found, then the absolute path to the exec is returned
})
// or promise
which('node').then(resolvedPath => { ... }).catch(er => { ... not found ... })
// sync usage
// throws if not found
var resolved = which.sync('node')
// if nothrow option is used, returns null if not found
resolved = which.sync('node', {nothrow: true})
// Pass options to override the PATH and PATHEXT environment vars.
which('node', { path: someOtherPath }, function (er, resolved) {
if (er)
throw er
console.log('found at %j', resolved)
})
Same as the BSD which(1)
binary.
usage: which [-as] program ...
You may pass an options object as the second argument.
path
: Use instead of the PATH
environment variable.pathExt
: Use instead of the PATHEXT
environment variable.all
: Return all matches, instead of just the first one. Note that
this means the function returns an array of strings instead of a
single string.2.0.2
node-which
FAQs
Like which(1) unix command. Find the first instance of an executable in the PATH.
The npm package which receives a total of 76,894,293 weekly downloads. As such, which popularity was classified as popular.
We found that which demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.